package com.briup.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * 词频统计
 * 统计文件中单词出现的个数
 */
public class WordCount extends Configured implements Tool {

    /**
     * 自定义map
     * 切割v1 根据逗号切割
     * 输出每个单词和 (IntWritable)1
     */
    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        //        定义一个常量  IntWritable类型的1
        private final IntWritable I = new IntWritable(1);

        @Override
//        每进来一组k v  就执行一次
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//            将读取到v1变成字符串  并义逗号进行切割 得到字符串数组
            String[] words = value.toString().split("[,]");
//            遍历数组
            for (int i = 0; i < words.length; i++) {
//                输出每一个单词和数字常量1
                context.write(new Text(words[i]), I);
            }
        }
    }

    public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int  num=0;
            for (IntWritable value : values) {
                num++;
//                num+=value.get();
            }
//            k2=k3  单词   v3  单词出现的次数
            context.write(key,new IntWritable(num));

        }
    }

    @Override
    public int run(String[] args) throws Exception {
//        获取配置对象
        Configuration conf = getConf();
//        从配置对象获取外部参数
        String in = conf.get("in");
        String out = conf.get("out");
//        构建 job对象
        Job job = Job.getInstance(conf);
//        job对象的基础属性设置
        job.setJobName("wordCount");
        job.setJarByClass(this.getClass());
//        构建输入输出路径
        Path inPath = new Path(in);
        Path outPath = new Path(out);
//        job的组件部分
//        输入处理器 默认的就是TextInputFormat 所以可以不设置
        job.setInputFormatClass(TextInputFormat.class);
//        告诉输入处理器 当前job要处理那个输入文件
        TextInputFormat.addInputPath(job,inPath);
//        设置job要执行的Mapper类型
        job.setMapperClass(WordCountMapper.class);
//        设置k2 v2 的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
//        设置reduceTask个数  默认是1  可以不设置
        job.setNumReduceTasks(1);
//        设置分区器  reduce个数为 1   分区无意义
        job.setPartitionerClass(HashPartitioner.class);
//        job 设置 reduce类型
        job.setReducerClass(WordCountReducer.class);
//        设置 k3  v3  的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
//        设置输出处理器  默认的是TextOutputFormat 所以也可以不设置
        job.setOutputFormatClass(TextOutputFormat.class);
//        告诉输出处理器当前job他要输出的路径
        TextOutputFormat.setOutputPath(job,outPath);
//        提交job


        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new WordCount(), args);
    }
}
